﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace WiTeach.Tools
{
    public static class SqlHelper
    {
        /// <summary>
        /// 获取数据连接字符串
        /// </summary>
        /// <returns>连接字符串</returns>
        public static string GetConnectionString()
        {
            return ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
            //return "";
        }

        /// <summary>
        /// 执行查询并将结果返回至DataTable中
        /// </summary>
        /// <param name="strSql">查询语句</param>
        /// <param name="parameters">可能带的参数</param>
        /// <returns>返回一张查询结果表</returns>
        public static DataTable ExecuteDataTable(string strSql, params MySqlParameter[] parameters)
        {
            using (MySqlConnection conn = new MySqlConnection(GetConnectionString()))
            {
                conn.Open();
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = strSql;
                    cmd.Parameters.AddRange(parameters);
                    DataSet ds = new DataSet();
                    using (MySqlDataAdapter adapter = new MySqlDataAdapter(cmd))
                    {
                        adapter.Fill(ds);
                        return ds.Tables[0];
                    }
                }
            }
        }

        /// <summary>
        /// (重载)执行查询并将结果返回至DataTable中
        /// </summary>
        /// <param name="strSql">查询语句</param>
        /// <returns>返回一张查询结果表</returns>
        public static DataTable ExecuteDataTable(string strSql)
        {
            MySqlConnection conn = new MySqlConnection(GetConnectionString());
            MySqlCommand cmd = new MySqlCommand();
            DataTable dt =null;
            using (conn)
            {
                try
                {
                    conn.Open();
                    using (cmd)
                    {
                        cmd.Connection = conn;
                        cmd.CommandText = strSql;
                        DataSet ds = new DataSet();
                        using (MySqlDataAdapter adapter = new MySqlDataAdapter(cmd))
                        {
                            adapter.Fill(ds);
                            dt = ds.Tables[0];
                        }
                    }
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex);
                    
                }
                return dt != null ? dt : null;
            }
        }


        /// <summary>
        /// 执行对数据的增删改操作
        /// </summary>
        /// <param name="strSql"></param>
        /// <param name="parameters"></param>
        public static void ExecuteNonQuery(string strSql, params SqlParameter[] parameters)
        {
            using (MySqlConnection conn = new MySqlConnection(GetConnectionString()))
            {
                conn.Open();
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = strSql;
                    cmd.Parameters.AddRange(parameters);
                    cmd.ExecuteNonQuery();
                }
            }
        }

        /// <summary>
        /// (重载)执行对数据的增删改操作
        /// </summary>
        /// <param name="strSql">查询语句</param>
        public static void ExecuteNonQuery(string strSql)
        {
            MySqlConnection conn = new MySqlConnection(GetConnectionString());
            MySqlCommand cmd = new MySqlCommand();
            if (conn == null || cmd == null)
                return ;
            try
            {
                using (conn)
                {
                    conn.Open();
                    using (cmd)
                    {
                        cmd.Connection = conn;
                        cmd.CommandText = strSql;
                        cmd.ExecuteNonQuery();
                    }
                }
            }catch(Exception ex)
            {
                Console.WriteLine(ex);
            }
            
        }

        /// <summary>
        /// 执行查询并返回结果集中第一行第一列的值
        /// </summary>
        /// <param name="strSql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static object ExecuteScalar(string strSql, params SqlParameter[] parameters)
        {
            MySqlConnection conn = new MySqlConnection(GetConnectionString());
            MySqlCommand cmd = new MySqlCommand();
            if (conn == null || cmd == null)
                return 0;
            using (conn)
            {
                conn.Open();
                using (cmd)
                {
                    cmd.Connection = conn;
                    cmd.CommandText = strSql;
                    cmd.Parameters.AddRange(parameters);
                    return cmd.ExecuteScalar();
                }
            }
        }

        /// <summary>
        /// (重载)执行查询并返回结果集中第一行第一列的值
        /// </summary>
        /// <param name="strSql">查询语句</param>
        /// <returns></returns>
        public static object ExecuteScalar(string strSql)
        {
            MySqlConnection conn = new MySqlConnection(GetConnectionString());
            MySqlCommand cmd = new MySqlCommand();
            object res = null;
            if (conn == null || cmd == null)
                return 0;
            using (conn)
            {
                try {
                    conn.Open();
                    cmd.Connection = conn;
                    cmd.CommandText = strSql;
                    res = cmd.ExecuteScalar();
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex);
                }

                return res !=null ? res : null;
              
            }

            // try
            //{

            //}
            //catch(Exception ex)
            //{
            //}
            //finally
            //{
 
            //}
            //using (MySqlConnection conn = new MySqlConnection(GetConnectionString()))
            //{
            //    conn.Open();
            //    using (MySqlCommand cmd = new MySqlCommand())
            //    {
            //        cmd.Connection = conn;
            //        cmd.CommandText = strSql;
            //        return cmd.ExecuteScalar();
            //    }
            //}
        }

    }


}
